<?php
namespace express;
class KdSearch{
    /**
     * 电商ID，快递鸟官网申请http://kdniao.com/reg
     */
    private $EBusinessID = 'test1461099';

    /**
     * 电商加密私钥，快递鸟提供，注意保管，不要泄漏
     */
    private $AppKey = '0ba0193a-d46c-4c90-b2c1-cf1187085e86';
    /**
     * 请求url
     */
    private $ReqUrl = 'http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json';

    
    //---------------------------------------------
     
    /**
     * Json方式 查询订单物流轨迹
     * 
     * @param string $kdType  快递类型
     * @param string $kdCode  物流单号
     * @param string $orderCode  订单号（默认为空）
     */
    public function getOrderTracesByJson($kdType, $kdCode, $orderCode = null){
        
        $requestData= [
            'OrderCode' => $orderCode,
            'ShipperCode' => $kdType,
            'LogisticCode' => $kdCode
        ];
        $requestData = json_encode($requestData);
        $datas = array(
            'EBusinessID' => $this->EBusinessID,
            'RequestType' => '1002',
            'RequestData' => urlencode($requestData) ,
            'DataType' => '2',
        );
        $datas['DataSign'] = $this->encrypt($requestData, $this->AppKey);
        $result = $this->sendPost($this->ReqUrl, $datas);	
        
        //根据公司业务处理返回的信息......
        
        return $result;
    }
     
    /**
     *  post提交数据 
     * @param  string $url 请求Url
     * @param  array $datas 提交的数据 
     * @return url 响应返回的html
     */
    function sendPost($url, $datas){
        $temps = array();	
        foreach ($datas as $key => $value) {
            $temps[] = sprintf('%s=%s', $key, $value);		
        }	
        $post_data = implode('&', $temps);
        $url_info = parse_url($url);
        if(empty($url_info['port']))
        {
            $url_info['port']=80;	
        }
        $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
        $httpheader.= "Host:" . $url_info['host'] . "\r\n";
        $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
        $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
        $httpheader.= "Connection:close\r\n\r\n";
        $httpheader.= $post_data;
        $fd = fsockopen($url_info['host'], $url_info['port']);
        fwrite($fd, $httpheader);
        $gets = "";
        $headerFlag = true;
        while (!feof($fd)) {
            if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
                break;
            }
        }
        while (!feof($fd)) {
            $gets.= fread($fd, 128);
        }
        fclose($fd);  
        
        return $gets;
    }
    
    /**
     * 电商Sign签名生成
     * @param data 内容   
     * @param appkey Appkey
     * @return DataSign签名
     */
    function encrypt($data, $appkey) {
        return urlencode(base64_encode(md5($data.$appkey)));
    }

}
